<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包应用，防抖函数</title>
</head>
<body>
    <input type="text" id='input'>
</body>
</html>

<script>

    let handle = function(val){
        /*逻辑体*/
        console.log(val);
    }

    function delayPrint(delay, callback){
        let timer = null;

        return function(value){/*注意点一：在返回该函数时无法确定的参数都要在这里接收而不是在父级形参中接收。例如在本例中，返回该函数时无法确定keyup的值，所以在这里要接收。*/
            if(timer){
                clearInterval(timer);
            }

            timer = setTimeout(function(){
                callback(value) /*注意点二：这里是function(){callback(val)}，而不是直接放callback(val)，直接放callback(val)是函数调用。*/
            },delay);
        }

    }

    let func = delayPrint(1000, handle);

    document.getElementById('input').addEventListener('keyup',function(e){
        //console.log(e.target.value);
        let val = e.target.value;
        func(val)
    })
</script>